package com.ruoyi.water.controller;

import java.util.List;
import java.time.LocalDate;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.water.domain.AreaWarn;
import com.ruoyi.water.service.IAreaWarnService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 区域告警Controller
 *
 * @author wujiong
 * @date 2025-09-22
 */
@RestController
@RequestMapping("/water/AreaWarn")
public class AreaWarnController extends BaseController
{
    @Autowired
    private IAreaWarnService areaWarnService;

    /**
     * 查询区域告警列表
     */
    @PreAuthorize("@ss.hasPermi('water:AreaWarn:list')")
    @GetMapping("/list")
    public TableDataInfo list(AreaWarn areaWarn)
    {
        startPage();
        LambdaQueryWrapper<AreaWarn> queryWrapper = new LambdaQueryWrapper<>(areaWarn);
        List<AreaWarn> list = areaWarnService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出区域告警列表
     */
    @PreAuthorize("@ss.hasPermi('water:AreaWarn:export')")
    @Log(title = "区域告警", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AreaWarn areaWarn)
    {
        LambdaQueryWrapper<AreaWarn> queryWrapper = new LambdaQueryWrapper<>(areaWarn);
        List<AreaWarn> list = areaWarnService.list(queryWrapper);
        ExcelUtil<AreaWarn> util = new ExcelUtil<AreaWarn>(AreaWarn.class);
        util.exportExcel(response, list, "区域告警数据");
    }

    /**
     * 获取区域告警详细信息
     */
    @PreAuthorize("@ss.hasPermi('water:AreaWarn:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(areaWarnService.getById(id));
    }

    /**
     * 新增区域告警
     */
    @PreAuthorize("@ss.hasPermi('water:AreaWarn:add')")
    @Log(title = "区域告警", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AreaWarn areaWarn)
    {
        return toAjax(areaWarnService.save(areaWarn) ? 1 : 0);
    }

    /**
     * 修改区域告警
     */
    @PreAuthorize("@ss.hasPermi('water:AreaWarn:edit')")
    @Log(title = "区域告警", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AreaWarn areaWarn)
    {
        return toAjax(areaWarnService.updateById(areaWarn) ? 1 : 0);
    }

    /**
     * 删除区域告警
     */
    @PreAuthorize("@ss.hasPermi('water:AreaWarn:remove')")
    @Log(title = "区域告警", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(areaWarnService.removeByIds(List.of(ids)) ? ids.length : 0);
    }

    /**
     * 按日期范围生成区域告警
     * 新增接口：调用服务层的告警生成逻辑
     */
    @PreAuthorize("@ss.hasPermi('water:AreaWarn:generate')")
    @Log(title = "区域告警", businessType = BusinessType.OTHER)
    @PostMapping("/generateByDateRange")
    public AjaxResult generateByDateRange(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate)
    {
        // 调用服务层方法生成告警
        boolean result = areaWarnService.generateAreaWarnByDateRange(startDate, endDate);
        if (result) {
            return AjaxResult.success("区域告警生成成功");
        } else {
            return AjaxResult.error("区域告警生成失败，请检查日志");
        }
    }
}
